package com.mmall.mmall002.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mmall.mmall002.entity.User;
import com.mmall.mmall002.service.CartService;
import com.mmall.mmall002.service.UserAddressService;
import com.mmall.mmall002.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙菲
 * @since 2021-03-02
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CartService cartService;

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 登录的方法
     */
    @PostMapping("/login")
    public  String login(String loginName, String password,HttpSession session){
        //查询条件
        QueryWrapper qw = new QueryWrapper();
        qw.eq("login_name",loginName);
        qw.eq("password",password);
        //根据登录名和查询用户
        User user =  userService.getOne(qw);
        System.out.println("user的信息是:"+user);
        //判断用户是否存在
        if(user != null){
            session.setAttribute("user",user);
            return "redirect:/productCategory/list";   //需要重定向到ProductCategoryController    productCategory的list方法
        }else{
            return "login";
        }
    }


    /**
     * 注销的方法
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();  //将session设置为失效
        return "login";
    }

    /**
     * 用户注册的方法
     */
    @PostMapping("/register")
    public String register(User user, Model model){
        boolean result = false;
        try {
            //将用户存储到数据库中  判断是否存储成功
            result = userService.save(user);
        } catch (Exception e) {
            model.addAttribute("error", user.getLoginName()+"已存在!请重新输入");
            return "register";
        }
        //判断是否注册成功，成功跳转到登录界面
        if(result)return "login";
        //失败返回注册界面
        return "register";
    }

    /**
     * 查询用户信息
     */
    @GetMapping("/userInfo")
    public ModelAndView userInfo(HttpSession session){
        ModelAndView modelAndView  = new ModelAndView();
        modelAndView.setViewName("userInfo");

        //获得登录的用户
        User user = (User) session.getAttribute("user");
        if(user == null){
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",new ArrayList<>());
        }else{
            //根据用户编号查询出来购物车集合
            modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        }
        return modelAndView;
    }

    /**
     * 地址管理  根据用户编号查询地址集合
     */
    @GetMapping("/userAddressList")
    public ModelAndView userAddressList(HttpSession session){
        ModelAndView modelAndView  = new ModelAndView();
        modelAndView.setViewName("userAddressList");

        //获得登录的用户
        User user = (User) session.getAttribute("user");
        //根据用户编号查询出来购物车集合
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));

        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id",user.getId());
        //根据用户编号查询地址集合
        modelAndView.addObject("addressList",userAddressService.list(qw));

        return modelAndView;
    }

}

